package com.code.cheney.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.code.cheney.dao.IUserDao;
import com.code.cheney.po.UserInfo;

/**   
 *    
 * Project: ssh_Maven   
 * Class: UserDAO   
 * Description: 
 * @author: Cheney   
 * @Date 2016年4月9日 下午1:55:54
 * @version 1.0
 *    
 */
@Repository
public class UserDAO implements IUserDao {

	@Autowired
	private SessionFactory sessionFactory;

	private Session getCurrentSession() {
		return this.sessionFactory.openSession();
	}
	
	@Override
	public void insert(UserInfo ui) {
		Session session = this.getCurrentSession();
		session.save(ui);
		session.flush();
	}

	@Override
	public List<UserInfo> query() {
		Session session = this.getCurrentSession();
		List<UserInfo> l = session.createQuery("from UserInfo").list();
		session.flush();
		return l;
	}

	@Override
	public UserInfo get(int id) {
		Session session = this.getCurrentSession();
		UserInfo userInfo = (UserInfo)session.get(UserInfo.class, id);
		session.flush();
		return userInfo;
	}

}
